package cn.anline.ann_wp

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.util.Log
import com.bigkoo.alertview.AlertView
import com.bigkoo.alertview.OnItemClickListener

/**
 * Created by luozh on 2017/7/12.
 */
class WelcomeActivity : Activity() {
    /**
     * 需要android6.0以上处理的权限
     */
    private val PERMISSIONS = arrayOf(
            //            Manifest.permission.ACCESS_COARSE_LOCATION,
//            Manifest.permission.ACCESS_FINE_LOCATION,

            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            //            Manifest.permission.READ_EXTERNAL_STORAGE,

//            Manifest.permission.CAMERA,

            //            Manifest.permission.CALL_PHONE,

//            Manifest.permission.WRITE_CONTACTS,

//            Manifest.permission.RECORD_AUDIO,

//            Manifest.permission.BODY_SENSORS,

            Manifest.permission.SET_WALLPAPER)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        // android系统大于等于6.0时需要处理时权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestNeedPermission()
        } else {
            goToMain()
        }
    }

    /**
     * 进入首页面
     */
    private fun goToMain() {
        //        判断是否引导动画
        Handler().postDelayed({
            val intent = Intent(this@WelcomeActivity, MainActivity::class.java)
            startActivity(intent)
            this@WelcomeActivity.finish()
        }, 1200)
    }

    /**
     * 判断是否需要进行权限的请求
     */
    private fun requestNeedPermission() {
        var temp = false
        for (permission in PERMISSIONS) {
            if (!hasPermissionGranted(permission)) {
                temp = true
                break
            }
        }
        if (temp) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, 0)
        } else {
            goToMain()
        }
    }

    /**
     * 判断该权限是否已经授权

     * @param permission
     * *
     * @return
     */
    private fun hasPermissionGranted(permission: String): Boolean {
        return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 0) {
            var temp = false
            for (i in grantResults.indices) {
                if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                    AlertView(
                            "权限不足",
                            "请到安卓‘系统设置->>应用程序->>百米账房’应用详情页面点击‘权限’勾选所有权限！本软件不获取root权限，请放心授权，以保障程序正常运行！ ", null,
                            arrayOf("取消", "授权"), null,
                            this@WelcomeActivity,
                            AlertView.Style.Alert,
                            OnItemClickListener { o, position ->
                                fun onItemClick(o: Any, position: Int) {
                                    Log.i("点击事件", "" + position)
                                    if (position == 1) {
                                        val localIntent = Intent()
                                        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                        if (Build.VERSION.SDK_INT >= 9) {
                                            localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                                            localIntent.data = Uri.fromParts("package", packageName, null)
                                        } else if (Build.VERSION.SDK_INT <= 8) {
                                            localIntent.action = Intent.ACTION_VIEW
                                            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails")
                                            localIntent.putExtra("com.android.settings.ApplicationPkgName", packageName)
                                        }
                                        startActivity(localIntent)
                                    }
                                }
                            }
                    ).show()

                    temp = true
                    break
                }
            }

            if (temp) {
                requestNeedPermission()
            } else {
                goToMain()
            }
        }
    }
}